package com.qww.exec08;

public enum  ConnectionManager {
    INSTANCE;
    private int initCapacity = 6;
    private Connection[] conns = new Connection[initCapacity];
    private int index;
    private static int size;

    public Connection[] getConns() {
        return size==0 ? null : conns;
    }

    public  Connection getConnection() {
        Connection conn = new Connection();
        // 数组扩容两倍
        if (size > initCapacity >> 1) copyArr();
        conns[size++] = conn;
        return conn;
    }

    private void copyArr() {
        Connection[] tmp = new Connection[initCapacity << 1];
        for (int i = 0; i < size; i++) {
            tmp[i] = conns[i];
        }
        conns = tmp;
    }

    public int size() {
        return size;
    }
}
